import { defineMock } from "./base";
import type { MockRequest } from "vite-plugin-mock-dev-server";

const MEASUREMENTS_URL = "measurements/phase-current/";

// 生成单条测量数据
function generateMeasurement(id: number) {
  return {
    id,
    measurements: [
      { name: "IL1", value: `${(Math.random() * 100).toFixed(3)} A` },
      { name: "IL1 angle", value: `${(Math.random() * 360).toFixed(3)} deg` },
      { name: "IL2", value: `${(Math.random() * 100).toFixed(3)} A` },
      { name: "IL2 angle", value: `${(Math.random() * 360).toFixed(3)} deg` },
      { name: "IL3", value: `${(Math.random() * 100).toFixed(3)} A` },
      { name: "IL3 angle", value: `${(Math.random() * 360).toFixed(3)} deg` },
      { name: "IN", value: `${(Math.random() * 100).toFixed(3)} A` },
      { name: "IN angle", value: `${(Math.random() * 360).toFixed(3)} deg` },
    ],
    createTime: "2025-05-28 12:00:00",
    updateTime: "2025-05-28 12:00:00",
  };
}

// 内部数据源
let measurementList = Array.from({ length: 100 }).map((_, id) => generateMeasurement(id));

export default defineMock([
  // 列表查询
  {
    url: `${MEASUREMENTS_URL}list`,
    method: ["GET"],
    body: (req: MockRequest) => {
      // 支持分页参数 page/pageSize
      const { page = 1, pageSize = 20 } = req.query || {};
      const start = (Number(page) - 1) * Number(pageSize);
      const end = start + Number(pageSize);
      const data = measurementList.slice(start, end);
      return {
        code: "00000",
        data: {
          list: data,
          total: measurementList.length,
        },
        msg: "ok",
      };
    },
  },
  // 使用 offset/limit 的分页查询
  {
    url: `${MEASUREMENTS_URL}offset-list`,
    method: ["GET"],
    body: (req: MockRequest) => {
      // 支持分页参数 offset/limit
      const { offset = 0, limit = 20 } = req.query || {};
      const start = Number(offset);
      const end = start + Number(limit);
      const data = measurementList.slice(start, end);
      return {
        code: "00000",
        data: {
          list: data,
          total: measurementList.length,
        },
        msg: "ok",
      };
    },
  },
  // 批量删除
  {
    url: `${MEASUREMENTS_URL}batch-delete`,
    method: ["POST"],
    body: (req: MockRequest) => {
      const { ids } = req.body || {};
      if (!Array.isArray(ids)) {
        return { code: "10001", msg: "参数错误，缺少ids数组" };
      }
      measurementList = measurementList.filter((item) => !ids.includes(item.id));
      return { code: "00000", data: null, msg: `已删除${ids.length}项` };
    },
  },
  // 批量更新
  {
    url: `${MEASUREMENTS_URL}batch-update`,
    method: ["POST"],
    body: (req: MockRequest) => {
      const { updates } = req.body || {};
      if (!Array.isArray(updates)) {
        return { code: "10001", msg: "参数错误，缺少updates数组" };
      }
      let updateCount = 0;
      updates.forEach((update: any) => {
        const idx = measurementList.findIndex((item) => item.id === update.id);
        if (idx !== -1) {
          measurementList[idx] = {
            ...measurementList[idx],
            ...update,
            updateTime: new Date().toISOString(),
          };
          updateCount++;
        }
      });
      return { code: "00000", data: null, msg: `已更新${updateCount}项` };
    },
  },
  // 使用 offset/limit 的分页查询
  {
    url: `device/measurements/phase-current/offset-list`,
    method: ["GET"],
    body: (req: MockRequest) => {
      // 支持分页参数 offset/limit
      const { offset = 0, limit = 20 } = req.query || {};
      const start = Number(offset);
      const end = start + Number(limit);
      const data = measurementList.slice(start, end);
      return {
        code: "00000",
        data: {
          list: data,
          total: measurementList.length,
        },
        msg: "ok",
      };
    },
  },
]);
